package com.zyx.kafkademo.producer;

import com.alibaba.fastjson.JSONObject;
import com.zyx.kafkademo.utils.KafkaUtils;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;

import java.util.Random;

/**
 * @author zyx
 * @since 2021/04/20 0:30
 */
public class JsonProducerDemo {
    public static void main(String[] args) {
        KafkaProducer<String, String> kafkaProducer = KafkaUtils.getKafkaProdocuer();

        Random random = new Random();
        String[] ids = {"1001", "1002", "1003", "1004", "1005", "1006"};
        int wordsLen = ids.length;

        for (int i = 0; i < 100; i++) {
            for (String id : ids) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("id", id);
                jsonObject.put("age", 10 + random.nextInt(20));
                String jsonStr = jsonObject.toJSONString();
                ProducerRecord<String, String> record = new ProducerRecord<>("user_age", jsonStr);
                kafkaProducer.send(record);
                System.out.println("producer >>" + jsonStr);
            }
        };


        kafkaProducer.close();
    }
}
